package com.wn.pro.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.common.entity.Stu;
import com.wn.pro.service.IStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-09-06
 */
@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private IStuService stuService;

    @GetMapping("/list")
    public List<Stu> getStuList() {
        return stuService.list();
    }

    @GetMapping("/page/{currentPage}/{pageSize}")
    public List<Stu> getStuListPage(@PathVariable("currentPage") int currentPage,
                                    @PathVariable("pageSize") int pageSize) {

        Page<Stu> page = new Page<>(currentPage, pageSize);
        stuService.page(page);
        long total = page.getTotal();
        System.out.println("total = " + total);
        List<Stu> records = page.getRecords();
        System.out.println("records = " + records);

        return records;
    }

    @GetMapping("/timeout")
    public String getTimeout(){

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "timeout...";

    }

    //拿到 yaml 中的 port
    @Value("${server.port}")
    private int port;

    @GetMapping("/test")
    public String getTest(){

        //测试 拿到 端口就可以
        Random random = new Random();
        int i = random.nextInt(1000);
        String result = "我是服务提供者:" + i + ": port: " + port;

        return result;

    }


}
